package m.jclecai.com.utils;

/**
 * Created by zd on 8/27/15.
 */

import android.text.TextUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DateUtil {

    /**
     * 将短时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
     *
     * @param strDate
     * @return
     */
    public static Date strToDate(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    public static long strToLong(String strTime) {
        return strToLong(strTime, "yyyy-MM-dd HH:mm:ss");
    }

    // string类型转换为long类型
    // strTime要转换的String类型的时间
    // formatType时间格式
    // strTime的时间格式和formatType的时间格式必须相同
    public static long strToLong(String strTime, String formatType) {
        Date date = strToDate(strTime, formatType); // String类型转成date类型
        if (date == null) {
            return 0;
        } else {
            return date.getTime(); // date类型转成long类型
        }
    }


    /**
     * 将短时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
     *
     * @param strDate
     * @return
     */
    public static Date strToDate(String strDate, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        Date date = null;
        try {
            date = formatter.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
            date = new Date(System.currentTimeMillis());
        }
        return date;
    }

    /**
     * 将短时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
     *
     * @param strDate
     * @return
     */
    public static String formatDate(String strDate, String format) {
        strDate = TextUtils.isEmpty(strDate) ? getCurrTime() : strDate;
        Date date = strToDate(strDate, format);
        SimpleDateFormat df = new SimpleDateFormat(format);//设置日期格式
        return df.format(date);//
    }


    /**
     * 获取当前日期
     *
     * @return 时间格式 yyyy-MM-dd HH:mm:ss
     */
    public static String getCurrTime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        return df.format(new Date(System.currentTimeMillis()));// new Date()为获取当前系统时间
    }

    /**
     * 获取当前日期
     *
     * @param format 根据输入的格式获取时间  yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String getCurrTime(String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);//设置日期格式
        return df.format(new Date());// new Date()为获取当前系统时间
    }

    /**
     * 获取当前日期
     *
     * @param format 根据输入的格式获取时间  yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String formatDate(Date date, String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);//设置日期格式
        return df.format(date);// new Date()为获取当前系统时间
    }

    /**
     * 两时间的天数差
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static Long getDaysBetween(Date startDate, Date endDate) {
        Calendar endC = Calendar.getInstance();
        endC.setTime(startDate);
        endC.set(Calendar.HOUR_OF_DAY, 0);
        endC.set(Calendar.MINUTE, 0);
        endC.set(Calendar.SECOND, 0);
        endC.set(Calendar.MILLISECOND, 0);

        Calendar toCalendar = Calendar.getInstance();
        toCalendar.setTime(endDate);
        toCalendar.set(Calendar.HOUR_OF_DAY, 0);
        toCalendar.set(Calendar.MINUTE, 0);
        toCalendar.set(Calendar.SECOND, 0);
        toCalendar.set(Calendar.MILLISECOND, 0);

        return Math.abs(toCalendar.getTime().getTime() - endC.getTime().getTime()) / (1000 * 60 * 60 * 24);
    }

    /**
     * video 两时间之间的差
     *
     * @param startS 起始时间
     * @param endS   截止时间
     */
    public static String distanceTime(String startS, String endS) {
        String retval = "";
        Date start = strToDate(startS);
        Date end = strToDate(endS);
        long i = getDaysBetween(start, end);
        Calendar endC = Calendar.getInstance();
        endC.setTime(end);
        Calendar startC = Calendar.getInstance();
        startC.setTime(start);
        if (i > 0) {
            if (endC.get(Calendar.YEAR) - (startC.get(Calendar.YEAR)) > 0) {//与当前时间相差一年
                System.out.println(startC.get(Calendar.YEAR) + "-" + (startC.get(Calendar.MONTH) + 1) + "-" + startC.get(Calendar.DATE));
                retval = startC.get(Calendar.YEAR) + "-" + (startC.get(Calendar.MONTH) + 1) + "-" + startC.get(Calendar.DATE);
            } else {
                System.out.println((startC.get(Calendar.MONTH) + 1) + "-" + startC.get(Calendar.DATE));
                retval = (startC.get(Calendar.MONTH) + 1) + "-" + startC.get(Calendar.DATE);
            }
        } else {
            if (startC.get(Calendar.HOUR) > 12) {
                System.out.println(startC.get(Calendar.HOUR) + ":" + startC.get(Calendar.MINUTE) + " pm");
                retval = startC.get(Calendar.HOUR) + ":" + startC.get(Calendar.MINUTE) + " pm";
            } else {
                System.out.println(startC.get(Calendar.HOUR) + ":" + startC.get(Calendar.MINUTE) + " am");
                retval = startC.get(Calendar.HOUR) + ":" + startC.get(Calendar.MINUTE) + " am";
            }
//            System.out.println(startC.get(Calendar.HOUR_OF_DAY)+":"+startC.get(Calendar.MINUTE)+":"+startC.get(Calendar.SECOND));
        }
        return retval;
    }

    /**
     * video 指定时间与当前时间的差
     *
     * @param startS 起始时间
     */
    public static String distanceTime(String startS) {
        return distanceTime(startS, true);
    }


    /**
     * video 指定时间与当前时间的差
     *
     * @param startS 起始时间
     */
    public static String distanceTime(String startS, boolean bool) {
        String retval = "";
        startS = startS.replaceAll("T", " ");
        startS = startS.replaceAll("Z", " ");
        Date start = strToDate(startS);
        Date end = strToDate(getCurrTime());
        long i = getDaysBetween(start, end);
        Calendar endC = Calendar.getInstance();
        endC.setTime(end);
        Calendar startC = Calendar.getInstance();
        startC.setTime(start);
        if (i > 0) {
            if (endC.get(Calendar.YEAR) - (startC.get(Calendar.YEAR)) > 0) {//与当前时间相差一年
                System.out.println(startC.get(Calendar.YEAR) + "-" + (startC.get(Calendar.MONTH) + 1) + "-" + startC.get(Calendar.DATE));
                retval = startC.get(Calendar.YEAR) + "-" + (startC.get(Calendar.MONTH) + 1) + "-" + startC.get(Calendar.DATE);
            } else {
                System.out.println((startC.get(Calendar.MONTH) + 1) + "-" + startC.get(Calendar.DATE));
                retval = (startC.get(Calendar.MONTH) + 1) + "-" + startC.get(Calendar.DATE);
            }
        } else {
            if (startC.get(Calendar.HOUR) > 12) {
                System.out.println(startC.get(Calendar.HOUR) + ":" + startC.get(Calendar.MINUTE) + " pm");
                retval = startC.get(Calendar.HOUR) + ":" + startC.get(Calendar.MINUTE);
                if (bool) retval += " pm";
            } else {
                System.out.println(startC.get(Calendar.HOUR) + ":" + startC.get(Calendar.MINUTE) + " am");
                retval = startC.get(Calendar.HOUR) + ":" + startC.get(Calendar.MINUTE);
                if (bool) retval += " am";
            }
//            System.out.println(startC.get(Calendar.HOUR_OF_DAY)+":"+startC.get(Calendar.MINUTE)+":"+startC.get(Calendar.SECOND));
        }
        return retval;
    }


    public static String utc2Local(String utcTime, String utcTimePatten,
                                   String localTimePatten) {
        SimpleDateFormat utcFormater = new SimpleDateFormat(utcTimePatten);
        utcFormater.setTimeZone(TimeZone.getTimeZone("UTC"));//时区定义并进行时间获取
        Date gpsUTCDate = null;
        try {
            gpsUTCDate = utcFormater.parse(TextUtils.isEmpty(utcTime) ? getCurrTime() : utcTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat localFormater = new SimpleDateFormat(localTimePatten);
        localFormater.setTimeZone(TimeZone.getDefault());
        String localTime = localFormater.format(gpsUTCDate.getTime());
        return localTime;
    }


    public static Long getDaysBetween(String startDate, String endDate) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date d1 = df.parse(startDate);
            Date d2 = df.parse(endDate);
            return Math.abs(d1.getTime() - d2.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0l;
    }

    public static boolean compare(String startDate, String endDate) {
        return compare(startDate, endDate, "yyyy-MM-dd HH:mm:ss");
    }

    public static boolean compare(String startDate, String endDate, String format) {
        DateFormat df = new SimpleDateFormat(format);
        try {
            Date d1 = df.parse(startDate);
            Date d2 = df.parse(endDate);
            return d1.getTime() > d2.getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 获取周几
     *
     * @return
     */
    public static int dayOfWeek() {
        Calendar calender = Calendar.getInstance();
        //一周第一天是否为星期天
        boolean isFirstSunday = (calender.getFirstDayOfWeek() == Calendar.SUNDAY);
        //获取周几
        int weekDay = calender.get(Calendar.DAY_OF_WEEK);
        //若一周第一天为星期天 则-1
        if (isFirstSunday) {
            weekDay = weekDay - 1;
            if (weekDay == 0)
                weekDay = 7;
        }
        return weekDay;
    }

    /**
     * 获取当前时间戳
     *
     * @return
     */
    public static long getSystemCurrentMills() {
        return System.currentTimeMillis();
    }

    /**
     * 获取当前时间戳
     *
     * @return
     */
    public static String getFormatTime(long time, String format) {
        return new SimpleDateFormat(format).format(time);
    }
}
